﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace KATS_Client.Server
{
    /// <summary>
    /// The local server. This recieves updates from the server about remote file actions [copying etc]
    /// </summary>
    public class Server : IServer, IDisposable
    {
        /// <summary>
        /// Contains information on progress updates
        /// </summary>
        public static event EventHandler<Client.FileActionProgress> ProgressUpdate;

        /// <summary>
        /// The server
        /// </summary>
        static ServiceHost serviceHost;

        /// <summary>
        /// Start/Restart the server
        /// </summary>
        public static void ReStart()
        {
            if (serviceHost != null)
            {
                int rv = ProgressReporting.Start("Closing server");
                serviceHost.Close();
                serviceHost = null;
                ProgressReporting.End(rv, "Server closed");
            }
            Start_Threaded();
        }
        /// <summary>
        /// Start the server in a new thread
        /// </summary>
        public static void Start_Threaded()
        {
            System.Threading.Thread QueueThread = new System.Threading.Thread(new System.Threading.ThreadStart(Start));
            QueueThread.Name = "Start local server thread";
            QueueThread.IsBackground = true;
            QueueThread.Start();
        }

        /// <summary>
        /// Start the server
        /// </summary>
        static void Start()
        {
            int rv = ProgressReporting.Start("Starting local server");
            serviceHost = new ServiceHost(typeof(Server));
            serviceHost.Open();
            ProgressReporting.End(rv, "Local server started");
        }

        /// <summary>
        /// Stop the server
        /// </summary>
        public static void StopServer()
        {
            if (serviceHost != null)
            {
                serviceHost.Close();
                serviceHost = null;
            }
        }

        /// <summary>
        /// Static constructor
        /// </summary>
        static Server()
        {
            Program.Exiting += new EventHandler<EventArgs>(Program_Exiting);
        }

        /// <summary>
        /// The program is exiting
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void Program_Exiting(object sender, EventArgs e)
        {
            StopServer();
        }

        /// <summary>
        /// The active server->client connections
        /// </summary>
        public static List<Server> ServerConnections = new List<Server>();

        /// <summary>
        /// Create a server object
        /// </summary>
        public Server()
        {
            ServerConnections.Add(this);
        }
        /// <summary>
        /// Update the progress percentage displayed
        /// </summary>
        /// <param name="progress">The percentage progress</param>
        public void Progress(int progress)
        {
            if (progress >= 0 && progress <= 100)
            {
                Client.FileActionsQueue.CurrentActionProgress.PercentageComplete = progress;
                Client.FileActionsQueue.CurrentActionProgress.Speed = null;

                if (ProgressUpdate != null)
                    ProgressUpdate(this, Client.FileActionsQueue.CurrentActionProgress);
            }
        }

        /// <summary>
        /// Updates the speed string of the current progress
        /// </summary>
        /// <param name="speed">The speed</param>
        public void Speed(string speed)
        {
            Client.FileActionsQueue.CurrentActionProgress.PercentageComplete = -1;
            Client.FileActionsQueue.CurrentActionProgress.Speed = speed;

            if (ProgressUpdate != null)
                ProgressUpdate(this, Client.FileActionsQueue.CurrentActionProgress);
        }

        /// <summary>
        /// Disconnect from the service
        /// </summary>
        public void Disconnect()
        {
        }

        /// <summary>
        /// Dispose of this object
        /// </summary>
        public void Dispose()
        {
            ServerConnections.Remove(this);
        }


    }
}
